package com.linkage.jz.bankruptcy.activity.Vote;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.Vote.Auth;
import com.linkage.jz.bankruptcy.bean.Vote.ProVote;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteVoterMyGroup;
import com.linkage.jz.bankruptcy.event.RefreshEvent;
import com.linkage.jz.bankruptcy.fragment.Vote.ProVoteDetailFragment;
import com.linkage.jz.bankruptcy.fragment.Vote.ProVoteDetailMyVoteFragment;
import com.linkage.jz.bankruptcy.fragment.Vote.ProVoteDetailRateFragment;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.utils.BundleUtils;
import com.linkage.jz.bankruptcy.utils.LogUtil;
import com.linkage.jz.bankruptcy.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author AFAP
 * 表决详情
 */
public class ProVoteVoteDetailActivity extends ProVoteBaseActivity {

    private ProVote proVote;

    private RadioGroup radioGroup;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;
    private ProVoteDetailFragment proVoteDetailFragment;
    private ProVoteDetailRateFragment proVoteDetailRateFragment;
    private ProVoteDetailMyVoteFragment proVoteDetailMyVoteFragment;

    @Override
    public void initData() {
        proVote = BundleUtils.getProVote();
        /*proVoteDetailFragment = ProVoteDetailFragment.newInstance();
        proVoteDetailRateFragment = ProVoteDetailRateFragment.newInstance();
        proVoteDetailMyVoteFragment = ProVoteDetailMyVoteFragment.newInstance();*/
        fetchData();
    }

    @Override
    public void setListener() {
    }

    @Override
    public void initTitle() {
        ImageButton titleBack = findViewById(R.id.title_back);
        titleBack.setOnClickListener(this);
        ImageButton topMenu = findViewById(R.id.title_menu1);
        topMenu.setVisibility(View.INVISIBLE);
        topMenu.setOnClickListener(this);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_pro_vote_vote_detail);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        radioGroup = findViewById(R.id.radioGroup);
        radioButton1 = findViewById(R.id.radio1);
        radioButton2 = findViewById(R.id.radio2);
        radioButton3 = findViewById(R.id.radio3);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(fragmentManager == null) {
                    fragmentManager = getSupportFragmentManager();
                }
                fragmentTransaction = fragmentManager.beginTransaction();
                if (proVoteDetailFragment != null) {
                    fragmentTransaction.hide(proVoteDetailFragment);
                }
                if (proVoteDetailRateFragment != null) {
                    fragmentTransaction.hide(proVoteDetailRateFragment);
                }
                if (proVoteDetailMyVoteFragment != null) {
                    fragmentTransaction.hide(proVoteDetailMyVoteFragment);
                }
                switch (checkedId) {
                    case R.id.radio1:
                        radioButton1.setTextSize(18);
                        radioButton2.setTextSize(14);
                        radioButton3.setTextSize(14);

                        if (proVoteDetailFragment == null) {
                            proVoteDetailFragment = ProVoteDetailFragment.newInstance();
                            fragmentTransaction.add(R.id.fff, proVoteDetailFragment);
                        } else {
                            fragmentTransaction.show(proVoteDetailFragment);
                        }
                        fragmentTransaction.commit();
                        break;
                    case R.id.radio2:
                        radioButton1.setTextSize(14);
                        radioButton2.setTextSize(18);
                        radioButton3.setTextSize(14);

                        if (proVoteDetailRateFragment == null) {
                            proVoteDetailRateFragment = ProVoteDetailRateFragment.newInstance();
                            fragmentTransaction.add(R.id.fff, proVoteDetailRateFragment);
                        } else {
                            fragmentTransaction.show(proVoteDetailRateFragment);
                        }
                        fragmentTransaction.commit();
                        break;
                    case R.id.radio3:
                        /*if(proVote.getStatus() == 2 && proVote.getVote() != proVote.getTotal()) {
                            if(proVote.isGrouped()) {
                                Intent intent = new Intent(ProVoteVoteDetailActivity.this, ProVoteMyGroupsActivity.class);
                                intent.putExtra("voteId", proVote.getId());
                                intent.putExtra("creditorId", proVote.getCreditorId());
                                startActivityForResult(intent, ProVoteEditVoteActivity.REQUEST_CODE_VOTE);
                            }else {
                                Intent intent = new Intent(ProVoteVoteDetailActivity.this, ProVoteEditVoteActivity.class);
                                intent.putExtra("vote", false);
                                intent.putExtra("voteId", proVote.getId());
                                intent.putExtra("groupId", (proVote.getGroupInfo() != null && proVote.getGroupInfo().size() > 0) ? proVote.getGroupInfo().get(0).getId() : "");
                                startActivityForResult(intent, ProVoteEditVoteActivity.REQUEST_CODE_VOTE);
                            }
                        }else {*/
                            radioButton1.setTextSize(14);
                            radioButton2.setTextSize(14);
                            radioButton3.setTextSize(18);

                            if (proVoteDetailMyVoteFragment == null) {
                                proVoteDetailMyVoteFragment = ProVoteDetailMyVoteFragment.newInstance();
                                fragmentTransaction.add(R.id.fff, proVoteDetailMyVoteFragment);
                            } else {
                                fragmentTransaction.show(proVoteDetailMyVoteFragment);
                            }
                            fragmentTransaction.commit();
                        //}
                        break;
                }
            }
        });
    }

    private void refreshTab(ProVote mProVote) {
        BundleUtils.saveProVote(mProVote);
        fragmentManager = this.getSupportFragmentManager();
        if("1".equalsIgnoreCase(mProVote.getAuth().isViewVotingSchedule()) ||"true".equalsIgnoreCase(mProVote.getAuth().isViewVotingSchedule())) {

            if(mProVote.getResultStatus()==1) {
                radioButton2.setText("表决结果");
            }else{
                radioButton2.setText("表决进度");
            }
            radioButton2.setVisibility(View.VISIBLE);
        }else {
            radioButton2.setVisibility(View.GONE);
        }

        if("1".equalsIgnoreCase(mProVote.getAuth().getMyVote()) ||"true".equalsIgnoreCase(mProVote.getAuth().getMyVote())) {


            radioButton3.setVisibility(View.VISIBLE);
        }else {
            radioButton3.setVisibility(View.GONE);
        }

//        if("1".equalsIgnoreCase(mProVote.getAuth().isViewVotingDetails()) ||"true".equalsIgnoreCase(mProVote.getAuth().isViewVotingDetails())) {
//
//
//            radioButton1.setVisibility(View.VISIBLE);
//        }else {
//            radioButton1.setVisibility(View.GONE);
//        }


        List<ProVoteVoterMyGroup> list = mProVote.getGroupInfo();
        boolean tmpflag = false;
//        switch (mProVote.getStatus()) {
//            case 1://未开始
//                for(int i=0;i<list.size();i++)
//                {
//                    ProVoteVoterMyGroup proVoteVoterMyGroup = (ProVoteVoterMyGroup)list.get(i);
//                    if("1".equalsIgnoreCase(proVoteVoterMyGroup.isMeeting())||"true".equalsIgnoreCase(proVoteVoterMyGroup.isMeeting()))
//                    {
//                        tmpflag = true;
//                        break;
//                    }
//                }
//                if(tmpflag)
//                {
//                    radioButton3.setVisibility(View.VISIBLE);
//                }else{
//                    radioButton3.setVisibility(View.GONE);
//                }
//
//                break;
//            case 2://已开始
////                for(int i=0;i<list.size();i++)
////                {
////                    ProVoteVoterMyGroup proVoteVoterMyGroup = (ProVoteVoterMyGroup)list.get(i);
////                    if(proVoteVoterMyGroup.isMeeting())
////                    {
////                        tmpflag = true;
////                        break;
////                    }
////                }
////                if(tmpflag)
////                {
//                    radioButton3.setVisibility(View.VISIBLE);
////                }else{
////                    radioButton3.setVisibility(View.GONE);
////                }
//                break;
//            case 3://已结束
//            case 5://已通过
//            case 6://未通过
//
//                for(int i=0;i<list.size();i++)
//                {
//                    ProVoteVoterMyGroup proVoteVoterMyGroup = (ProVoteVoterMyGroup)list.get(i);
//                    if("1".equalsIgnoreCase(proVoteVoterMyGroup.isVote())||"true".equalsIgnoreCase(proVoteVoterMyGroup.isVote()))
//                    {
//                        tmpflag = true;
//                        break;
//                    }
//                }
//                if(tmpflag)
//                {
//                    radioButton3.setVisibility(View.VISIBLE);
//                }else{
//                    radioButton3.setVisibility(View.GONE);
//                }
//                break;
//            case 4://已撤销
//            default:
//                radioButton2.setVisibility(View.GONE);
//                radioButton3.setVisibility(View.GONE);
//                break;
//
//        }
        fragmentTransaction = fragmentManager.beginTransaction();
        proVoteDetailFragment = null;
        proVoteDetailRateFragment = null;
        proVoteDetailMyVoteFragment = null;
        //radioButton1.performClick();
        radioButton1.setTextSize(18);
        radioButton2.setTextSize(14);
        radioButton3.setTextSize(14);
//        radioButton1.setTextColor(getResources().getColor(R.color.black_3));

//        if("1".equalsIgnoreCase(mProVote.getAuth().isViewVotingDetails()) ||"true".equalsIgnoreCase(mProVote.getAuth().isViewVotingDetails())) {

            if (proVoteDetailFragment == null) {
                proVoteDetailFragment = ProVoteDetailFragment.newInstance();
                fragmentTransaction.add(R.id.fff, proVoteDetailFragment);
            } else {
                fragmentTransaction.show(proVoteDetailFragment);
            }
            fragmentTransaction.commit();
            radioButton1.setVisibility(View.VISIBLE);
//        }else {
//            radioButton1.setVisibility(View.GONE);
//            if (proVoteDetailFragment == null) {
//                proVoteDetailFragment = ProVoteDetailFragment.newInstance();
//                fragmentTransaction.add(R.id.fff, proVoteDetailFragment);
//            }
//        }


    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.title_menu1:
                finish();
                break;
        }
    }

    /**
     * 获取详情
     */
    private void fetchData() {
        if (BankruptcyApp.tokeninfo == null || BankruptcyApp.tokeninfo.getToken() == null) {
            return;
        }
        showLoading();
        String userToken = SPUtils.getInstance(Consts.SPNAME).getString("userToken", "");
        Network.getAPIService()
                .getVoteDetail_Creditor(userToken, proVote.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        hideLoading();
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            super.onNext(json);
                            return;
                        }
                        LogUtil.e("--response--", json.toString());
                        JsonObject data = json.getAsJsonObject("data").getAsJsonObject("voteInfo");
                        JsonObject auth = json.getAsJsonObject("data").getAsJsonObject("auth");
                        JsonArray groupInfoArray = json.getAsJsonObject("data").getAsJsonArray("groupInfo");
                        ProVote temp = ProVote.parse(data);
                        if(temp != null) {
                            temp.setAuth(Auth.parse(auth));
                            if(groupInfoArray!=null) {
                                temp.setGroupInfo(ProVoteVoterMyGroup.parse(groupInfoArray));
                                temp.setNoGroupData(false);
                            }else{
                                List<ProVoteVoterMyGroup> groupInfo = new ArrayList<ProVoteVoterMyGroup> ();
                                temp.setNoGroupData(true);
                                temp.setGroupInfo(groupInfo);
                            }

                            if(temp.getGroupInfo() != null && temp.getGroupInfo().size() > 0) {
                                temp.setCreditorId(temp.getGroupInfo().get(0).getCreditorId());
                            }
                            temp.setCreditorId(proVote.getCreditorId());
                                temp.setTotal(proVote.getTotal());
                                temp.setMeeting(proVote.getMeeting());
                                temp.setVote(proVote.getVote());
                                proVote = temp;


                                //测试
//                            temp.getAuth().setViewVotingDetails("0");
//                            temp.getAuth().setViewVotingSchedule("1");
//                            temp.getAuth().setMyVote("1");
//
//                            temp.getAuth().setDoVote("1");
//                            temp.getAuth().setMeetingVote("1");
//                            temp.getAuth().setJoinMeeting("1");

                                refreshTab(proVote);
//                            }
                        }
                    }

                    @Override
                    public void onCompleted() {
                        hideLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                    }
                });
    }

    public void refreshTab() {
        fetchData();
        RefreshEvent event = new RefreshEvent();
        event.setType(RefreshEvent.TYPE_VOTE);
        EventBus.getDefault().post(event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //adapter.getItem(viewPager.getCurrentItem()).onActivityResult(requestCode, resultCode, data);
        if(requestCode == ProVoteEditVoteActivity.REQUEST_CODE_VOTE && resultCode == RESULT_OK) {
            refreshTab();
        }
    }

}
